(******************************************************************************
* The Collection of Notifier objects.                                         *
******************************************************************************)
unit Notify;

interface

uses Windows,NotifyDef;

type
  TNotify = class
  protected
    hNotify : THandle;
    hOwner : THandle;
    Message,
    UParam : dword;
  public
    constructor Create(hEventObj : THandle);
    procedure Execute; virtual;
    property Owner : THandle read hOwner write hOwner;
    property Param : dword read UParam write UParam;
  end;

  TThreadNotify = class(TNotify)
  protected
  public
    constructor Create(hEventObj,hSender : THandle;
                       MsgID,UserParam : dword);
    procedure Execute; override;
  end;

  TWinNotify = class(TThreadNotify)
  public
    procedure Execute; override;
  end;

  TCallBackNotify = class(TThreadNotify)
  public
    procedure Execute; override;
  end;

  TCallEventNotify = class(TThreadNotify)
  private
    fOnNotify : TNotifierEvent;
  public
    constructor Create(hEventObj : TNotifierEvent; hSender : THandle;
                       MsgID,UserParam : dword);
    property OnNotify : TNotifierEvent read fOnNotify write fOnNotify;
    procedure Execute; override;
  end;

function MakeNotifier(hEventObj,hSender : THandle;
                      EventType : byte; MsgID,UserParam : dword) : TNotify;


implementation


function MakeNotifier(hEventObj,hSender : THandle;
                      EventType : byte; MsgID,UserParam : dword) : TNotify;
  begin
    case EventType of
      tnEvent     : result := TNotify.Create(hEventObj);
      tnThreadMsg :
        result := TThreadNotify.Create(hEventObj, hSender, MsgID, UserParam);
      tnWinMsg    :
        result := TWinNotify.Create(hEventObj, hSender, MsgID, UserParam);
      tnCallBack  :
        result := TCallBackNotify.Create(hEventObj, hSender, MsgID, UserParam);
      //tnCallEvent :
      //  result := TCallEventNotify.Create(hEventObj, hSender, MsgID, UserParam);
    else result := nil;
    end;
  end;


(*** TNotify ***)

constructor TNotify.Create(hEventObj : THandle);
  begin
    hNotify := hEventObj;
  end;

procedure TNotify.Execute;
  begin
    SetEvent(hNotify);
  end;


(*** TThreadNotify ***)

constructor TThreadNotify.Create(hEventObj,hSender : THandle;
                                 MsgID,UserParam : dword);
  begin
    inherited Create(hEventObj);
    Owner := hSender;
    Message := MsgID;
    UParam := UserParam;
  end;

procedure TThreadNotify.Execute;
  begin
    PostThreadMessage(hNotify, Message, UParam, hOwner);
  end;

(*** TWinNotify ***)

procedure TWinNotify.Execute;
  begin
    PostMessage(hNotify, Message, UParam, hOwner);
  end;

(*** TCallbackNotify ***)

procedure TCallbackNotify.Execute;
  begin
    TNotifierProc(hNotify)(hOwner, Message, UParam);
  end;

(*** TCalleventNotify ***)

constructor TCalleventNotify.Create(hEventObj : TNotifierEvent; hSender : THandle;
                    MsgID,UserParam : dword);
  begin
    OnNotify := hEventObj;
    Owner := hSender;
    Message := MsgID;
    UParam := UserParam;
  end;

procedure TCalleventNotify.Execute;
  begin
    if assigned(OnNotify) then OnNotify(TObject(Owner), Message, UParam);
  end;

end.
